package com.practise.rabbitmqdemo.listen;

import com.practise.rabbitmqdemo.config.DeadTTLDirectRabbitConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class DeadTTLDirectReceiver {


    @RabbitListener(queues = DeadTTLDirectRabbitConfig.DEAD_Q2,ackMode ="MANUAL")
    public void processQueue(String msg , Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        try {
            channel.basicAck(deliveryTag, true);
            log.info(" TTL实现死信队列 deliveryTag: {} 监听{} msg={}",deliveryTag,DeadTTLDirectRabbitConfig.DEAD_Q2,msg);
        }catch (IOException | ArithmeticException exception) {
            // 有异常，将消息返回给Queue里，第三个参数requeue可以直接看出来，是否返回到Queue中
            System.out.println("TTL实现死信队列  有异常  ");
            channel.basicNack(deliveryTag, true, true);
        }
    }

    @RabbitListener(queues = DeadTTLDirectRabbitConfig.DEAD_Q3,ackMode ="MANUAL")
    public void processQueue3(String msg , Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        try {
            channel.basicAck(deliveryTag, true);
            log.info(" 插件方式实现死信队列 deliveryTag: {} 监听{} msg={}",deliveryTag,DeadTTLDirectRabbitConfig.DEAD_Q3,msg);
        }catch (IOException | ArithmeticException exception) {
            // 有异常，将消息返回给Queue里，第三个参数requeue可以直接看出来，是否返回到Queue中
            System.out.println("插件方式实现死信队列  有异常  ");
            channel.basicNack(deliveryTag, true, true);
        }
    }
}